package com.study.web;

import cn.hutool.core.util.IdUtil;
import com.study.api.ApiController.DataDTO.Stu;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.TcpClient;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * @author zhuzi
 * 2024/6/19 16:43
 */
@Slf4j
public class WebClientDemo {

    @Test
    public void d1() {
        WebClient webClient = WebClient.create("");

        webClient.get().accept(MediaType.APPLICATION_JSON).header("", "")
                //定义参数
                .attribute("", "").retrieve().bodyToMono(String.class)
                //阻塞
                .block();

    }

    @Test
    public void d2() {
        Stu stus = new Stu();
        //https://blog.csdn.net/A_art_xiang/article/details/127025263

        stus.setId(IdUtil.getSnowflakeNextId()).setAge(29).setName("透明-ABC");
        WebClient webClient = WebClient.create("http://127.0.0.1:8051/apa/c");
        Mono<Stu> stuMono = webClient.post().accept(MediaType.APPLICATION_JSON)
                .bodyValue(stus)//请求体
                .retrieve().bodyToMono(Stu.class).doOnError(Exception.class, err -> {  //处理异常
                    System.out.println(LocalDateTime.now() + "---发生错误：" + err.getMessage());
                });
        ;
        stuMono.subscribe(s -> {
            log.info("请求返回{}", s);
        }, throwable -> {
            System.out.println(throwable.getMessage());
            System.out.println("发生错误");
        });
    }

    @Test
    public void d3() {
        //构建客户端
        WebClient client = WebClient.builder()
                .baseUrl("http://localhost:8080")
                .defaultCookie("cookieKey", "cookieValue")
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
                .build();
    }


    @Test
    public void d4TimeOut() {
        //默认是30S超时时间
        //HttpClient httpClient = HttpClient.create()
        //        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 设置连接超时为5秒
        //        .responseTimeout(Duration.ofMillis(5000)); // 设置响应超时为5秒

        HttpClient httpClient = HttpClient.create()
                .responseTimeout(Duration.ofSeconds(10));

        TcpClient tcpClient = TcpClient
                .create()
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
                .doOnConnected(connection -> {
                    connection.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS));
                    connection.addHandlerLast(new WriteTimeoutHandler(5000, TimeUnit.MILLISECONDS));
                });
    }


}
